//
//  MSFightScene.m
//  MagicSword
//
//  Created by Netease on 15/11/23.
//  Copyright © 2015年 Jonear. All rights reserved.
//

#import "MSFightScene.h"
#import "MSHeroInfo.h"
#import "MSEnemyInfo.h"
#import "MSProgressBar.h"
#import "MSFinghtActionInfo.h"
#import "MSGameManager.h"
#import "MSWeaponNode.h"
#import "MSMagicSprite.h"
#import "MSShadowLabel.h"
#import "MSUpArrowSprite.h"

@implementation MSFightScene {
    SKTextureAtlas *_heroAtlas;
    SKTextureAtlas *_enemyAtlas;
    SKTextureAtlas *_backgroundAtlas;
    MSProgressBar  *_heroProgressBar;
    MSProgressBar  *_enemyProgressBar;
    SKLabelNode    *_heroNameLabel;
    SKLabelNode    *_enemyNameLabel;
    SKSpriteNode   *_redNode;
    MSWeaponNode   *_weaponNode;
    MSShadowLabel    *_weaponNameLabel;
    MSWeaponNode   *_weaponNode2;
    MSShadowLabel    *_weaponNameLabel2;
    MSCompletionBlock _block;
    float             _delayAttakTime;
    
    NSArray *heroAtlasName;
    NSArray *enemyAtlasName;
    NSString *textureHeroNormalName;
    NSString *textureEnemyNormalName;
    
    double    _heroActionDuration;
    double    _enemyActionDuration;
    double    _actionDuration;
    
    MSUpArrowSprite  *_heroWeaponUpSprite;
    MSUpArrowSprite  *_enemyWeaponUpSprite;
}

- (id)initWithColor:(UIColor *)color size:(CGSize)size {
    self = [super initWithColor:color size:size];
    if (self) {
        self.zPosition = 66;
        _actionDuration = 0.1;
        _backgroundAtlas = [SKTextureAtlas atlasNamed:@"Battle_Backgrounds"];
        [self setTexture:[_backgroundAtlas textureNamed:@"Session_Battle_1"]];
        
        _heroProgressBar = [[MSProgressBar alloc] initWithSize:CGSizeMake(CGRectGetWidth(self.frame)/2-30, 25)];
        _heroProgressBar.position = CGPointMake(CGRectGetWidth(self.frame)/4, -88);
        _heroProgressBar.zPosition = 3;
        [self addChild:_heroProgressBar];
        
        _enemyProgressBar = [[MSProgressBar alloc] initWithSize:CGSizeMake(CGRectGetWidth(self.frame)/2-30, 25)];
        _enemyProgressBar.position = CGPointMake(-CGRectGetWidth(self.frame)/4, -88);
        _enemyProgressBar.zPosition = 3;
        [self addChild:_enemyProgressBar];
        
        _heroNameLabel = [[SKLabelNode alloc] initWithFontNamed:@"Helvetica"];
        _heroNameLabel.position = CGPointMake(CGRectGetWidth(self.frame)/2-20, -120);
        _heroNameLabel.horizontalAlignmentMode = SKLabelHorizontalAlignmentModeRight;
        _heroNameLabel.zPosition = 3;
        _heroNameLabel.fontSize = 13;
        [self addChild:_heroNameLabel];
        
        _enemyNameLabel = [[SKLabelNode alloc] initWithFontNamed:@"Helvetica"];
        _enemyNameLabel.position = CGPointMake(-CGRectGetWidth(self.frame)/2+20, -120);
        _enemyNameLabel.horizontalAlignmentMode = SKLabelHorizontalAlignmentModeLeft;
        _enemyNameLabel.zPosition = 3;
        _enemyNameLabel.fontSize = 13;
        [self addChild:_enemyNameLabel];
        
        _weaponNode = [[MSWeaponNode alloc] init];
        _weaponNode.position = CGPointMake(30, -115);
        _weaponNode.size = CGSizeMake(18, 18);
        _weaponNode.zPosition = 3;
        [_weaponNode setShapeNodeHidden:NO];
        [_weaponNode setCountTitleHidden:YES];
        [self addChild:_weaponNode];
        
        _weaponNameLabel = [[MSShadowLabel alloc] initWithFontNamed:@"Helvetica"];
        _weaponNameLabel.position = CGPointMake(45, -120);
        _weaponNameLabel.horizontalAlignmentMode = SKLabelHorizontalAlignmentModeLeft;
        _weaponNameLabel.zPosition = 3;
        _weaponNameLabel.fontSize = 12;
        _weaponNameLabel.fontColor = [UIColor whiteColor];
        [_weaponNameLabel setShadowColor:[UIColor darkGrayColor]];
        [self addChild:_weaponNameLabel];
        
        _heroWeaponUpSprite = [[MSUpArrowSprite alloc] init];
        _heroWeaponUpSprite.zPosition = 3;
        _heroWeaponUpSprite.size = CGSizeMake(15, 15);
        _heroWeaponUpSprite.position = CGPointMake(90, -115);
        [_heroWeaponUpSprite setHidden:YES];
        [self addChild:_heroWeaponUpSprite];
        
        _weaponNode2 = [[MSWeaponNode alloc] init];
        _weaponNode2.position = CGPointMake(-30, -115);
        _weaponNode2.size = CGSizeMake(18, 18);
        _weaponNode2.zPosition = 3;
        [_weaponNode2 setShapeNodeHidden:NO];
        [_weaponNode2 setCountTitleHidden:YES];
        [self addChild:_weaponNode2];
        
        _weaponNameLabel2 = [[MSShadowLabel alloc] initWithFontNamed:@"Helvetica"];
        _weaponNameLabel2.position = CGPointMake(-45, -120);
        _weaponNameLabel2.horizontalAlignmentMode = SKLabelHorizontalAlignmentModeRight;
        _weaponNameLabel2.zPosition = 3;
        _weaponNameLabel2.fontSize = 12;
        _weaponNameLabel2.fontColor = [UIColor whiteColor];
        [_weaponNameLabel2 setShadowColor:[UIColor darkGrayColor]];
        [self addChild:_weaponNameLabel2];
        
        _enemyWeaponUpSprite = [[MSUpArrowSprite alloc] init];
        _enemyWeaponUpSprite.zPosition = 3;
        _enemyWeaponUpSprite.size = CGSizeMake(15, 15);
        _enemyWeaponUpSprite.position = CGPointMake(-90, -115);
        [_enemyWeaponUpSprite setHidden:YES];
        [self addChild:_enemyWeaponUpSprite];
        
        _redNode = [SKSpriteNode spriteNodeWithColor:UIColorFromRGBA(0xff0000, 0.3) size:size];
        _redNode.position = CGPointMake(0, 0);
        _redNode.zPosition = 10;
        _redNode.alpha = 0;
        [self addChild:_redNode];
    }
    return self;
}

- (void)fightWithHeroInfo:(MSHeroInfo *)hero
                    enemy:(MSEnemyInfo *)enemy
               completion:(MSCompletionBlock)block {
    [self fightWithHeroInfo:hero enemy:enemy isHeroAttack:YES completion:block];
}

- (void)fightWithHeroInfo:(MSHeroInfo *)hero
                    enemy:(MSEnemyInfo *)enemy
             isHeroAttack:(BOOL)isHeroAttack
               completion:(MSCompletionBlock)block
{
    _block = block;
    heroAtlasName = @[@"HeroA_Detail", @"HeroB_Detail", @"HeroC_Detail", @"HeroD_Detail", @"HeroE_Detail"];
    enemyAtlasName = @[@"EnemyA_Detail", @"EnemyB_Detail", @"EnemyC_Detail", @"EnemyD_Detail", @"EnemyE_Detail"];
    if (hero.curWeapon) {
        [_weaponNode setWeaponInfo:hero.curWeapon];
        [_weaponNameLabel setText:hero.curWeapon.name];
    } else {
        if (hero.weaponList.count > 0) {
            hero.curWeapon = [hero.weaponList firstObject];
            [_weaponNode setWeaponInfo:hero.curWeapon];
            [_weaponNameLabel setText:hero.curWeapon.name];
        } else {
            [_weaponNode setHidden:YES];
            [_weaponNameLabel setHidden:YES];
        }
    }
    
    [_weaponNode2 setWeaponInfo:enemy.weaponInfo];
    [_weaponNameLabel2 setText:enemy.weaponInfo.name];
    
    if ([[MSGameManager shareManager] isWeaponWin:hero.curWeapon.type dWeapon:enemy.weaponInfo.type]) {
        [_heroWeaponUpSprite setHidden:NO];
        [_heroWeaponUpSprite runAction];
    } else if ([[MSGameManager shareManager] isWeaponWin:enemy.weaponInfo.type dWeapon:hero.curWeapon.type]) {
        [_enemyWeaponUpSprite setHidden:NO];
        [_enemyWeaponUpSprite runAction];
    }
    
    _heroAtlas = [SKTextureAtlas atlasNamed:heroAtlasName[hero.type]];
    textureHeroNormalName = [NSString stringWithFormat:@"TextureImage_%d", 0];
    SKSpriteNode *heroSprite = [SKSpriteNode spriteNodeWithTexture:[_heroAtlas textureNamed:textureHeroNormalName]];
    heroSprite.zPosition = 2;
    [self setHeroSpritePossion:heroSprite heroInfo:hero];
    [self addChild:heroSprite];
    
    _enemyAtlas = [SKTextureAtlas atlasNamed:enemyAtlasName[enemy.type]];
    textureEnemyNormalName = [NSString stringWithFormat:@"TextureImage_%d", 0];
    SKSpriteNode *enemySprite = [SKSpriteNode spriteNodeWithTexture:[_enemyAtlas textureNamed:textureEnemyNormalName]];
    enemySprite.zPosition = 2;
    [self setEnemySpritePossion:enemySprite enemyInfo:enemy];
    [self addChild:enemySprite];
    
    [_heroProgressBar setProgress:hero.HP/(float)hero.MaxHP];
    [_heroProgressBar.titleLabelNode setText:[NSString stringWithFormat:@"%d/%d", hero.HP, hero.MaxHP]];
    [_enemyProgressBar setProgress:enemy.HP/(float)enemy.MaxHP];
    [_enemyProgressBar.titleLabelNode setText:[NSString stringWithFormat:@"%d/%d", enemy.HP, enemy.MaxHP]];
    [_heroNameLabel setText:[NSString stringWithFormat:@"%@ lv.%d", hero.name, hero.level]];
    [_enemyNameLabel setText:enemy.name];
    
    NSInteger type = isHeroAttack? hero.type:enemy.type;
    NSInteger profession = isHeroAttack? hero.profession:enemy.weaponInfo.type;
    MSWeaponInfo *weaponInfo = isHeroAttack? hero.curWeapon:enemy.weaponInfo;
    SKAction *action = [self getActionWithIsHeroAttack:isHeroAttack type:type pro:profession weapon:weaponInfo];
    SKSpriteNode *sprite = nil;
    SKSpriteNode *otherSprite = nil;
    MSProgressBar *progressBar = nil;
    NSInteger HP = 1;
    NSInteger MAXHP = 1;
    BOOL isDoubleHit = [[MSGameManager shareManager] isCanDoubleHit:hero enemy:enemy isHeroAttack:isHeroAttack]; //连击
    BOOL isMiss      = NO;   //MISS
    BOOL isBackHit   = [[MSGameManager shareManager] isCanBackHit:hero enemy:enemy isHeroAttack:isHeroAttack];   //回击 默认50%几率 跟武器克性有关
    if (isHeroAttack) {
        sprite = heroSprite;
        otherSprite = enemySprite;
        progressBar = _enemyProgressBar;
        float missHp = [[MSGameManager shareManager] attackHPEnemy:hero enemy:enemy];
        HP = enemy.HP;
        MAXHP = enemy.MaxHP;
        isMiss = (missHp==0);
    } else {
        sprite = enemySprite;
        otherSprite = heroSprite;
        progressBar = _heroProgressBar;
        float missHp = [[MSGameManager shareManager] attackHPHero:hero enemy:enemy];
        HP = hero.HP;
        MAXHP = hero.MaxHP;
        isMiss = (missHp==0);
    }
    sprite.zPosition = 3;
    
    
    [sprite runAction:action completion:^{
        // 8扣血
        if (isMiss) {
             [self moreFinght:isDoubleHit
                    isBackHit:isBackHit
                 isHeroAttack:isHeroAttack
                         hero:hero
                        enemy:enemy
                   heroSprite:heroSprite
                  enemySprite:enemySprite
                           HP:HP];
        } else {
            [progressBar.titleLabelNode setText:[NSString stringWithFormat:@"%d/%d", HP, MAXHP]];
            [progressBar setProgressWithAniamtion:HP/(float)MAXHP completion:^{
                [self moreFinght:isDoubleHit
                       isBackHit:isBackHit
                    isHeroAttack:isHeroAttack
                            hero:hero
                           enemy:enemy
                      heroSprite:heroSprite
                     enemySprite:enemySprite
                              HP:HP];
            }];
        }
    }];
    if (isMiss) {
        [self performSelector:@selector(missShowWithPosition:) withObject:[NSValue valueWithCGPoint:CGPointMake(otherSprite.position.x, -20)] afterDelay:_delayAttakTime];
    }
    
}

- (void)setHeroSpritePossion:(SKSpriteNode *)heroSprite heroInfo:(MSHeroInfo *)hero {
    MSFinghtActionInfo *finghtInfo = [MSFinghtActionInfo infoWithHeroType:hero.type];
    
    heroSprite.position = CGPointMake(CGRectGetWidth(self.frame)/4, finghtInfo.spriteTop);
    [heroSprite setScale:5];
    _heroActionDuration = finghtInfo.actionDuration;
}
- (void)setEnemySpritePossion:(SKSpriteNode *)enemySprite enemyInfo:(MSEnemyInfo *)Enemy {
    MSFinghtActionInfo *finghtInfo = [MSFinghtActionInfo infoWithEnemyType:Enemy.type];
    
    enemySprite.position = CGPointMake(-CGRectGetWidth(self.frame)/4, finghtInfo.spriteTop);
    [enemySprite setScale:5];
    _enemyActionDuration = finghtInfo.actionDuration;
    
}

- (void)moreFinght:(BOOL)isDoubleHit
         isBackHit:(BOOL)isBackHit
      isHeroAttack:(BOOL)isHeroAttack
              hero:(MSHeroInfo *)hero
             enemy:(MSEnemyInfo *)enemy
        heroSprite:(SKSpriteNode *)heroSprite
       enemySprite:(SKSpriteNode *)enemySprite
                HP:(NSInteger)HP {
    if (isDoubleHit==NO && isBackHit==YES) {
        isHeroAttack = !isHeroAttack;
    }
    NSInteger type = isHeroAttack? hero.type:enemy.type;
    NSInteger profession = isHeroAttack? hero.profession:enemy.weaponInfo.type;
    MSWeaponInfo *weaponInfo = isHeroAttack? hero.curWeapon:enemy.weaponInfo;
    SKSpriteNode *sprite = isHeroAttack? heroSprite:enemySprite;
    if ((isDoubleHit||isBackHit) && HP>0) {
        SKAction *action = [self getActionWithIsHeroAttack:isHeroAttack type:type pro:profession weapon:weaponInfo];
        [self doubleHitShow:isHeroAttack hero:hero enemy:enemy sprite:sprite action:action isBack:NO];
    } else {
        [self finishAction];
    }
}

- (SKAction *)getActionWithIsHeroAttack:(BOOL)isHeroAttack type:(NSInteger)type pro:(MSHeroProfession)pro weapon:(MSWeaponInfo *)weapon{
    if (pro == MSHeroProfession_Magic) {
        return [self getMagicActionWithIsHeroAttack:isHeroAttack type:type weapon:weapon];
    } else if (pro == MSHeroProfession_Bow) {
        return [self getBowActionWithIsHeroAttack:isHeroAttack type:type];
    }
    
    return [self getActionWithIsHeroAttack:isHeroAttack type:type];
}

- (SKAction *)getMagicActionWithIsHeroAttack:(BOOL)isHeroAttack type:(NSInteger)type weapon:(MSWeaponInfo *)weapon {
    SKAction *action;
    if (isHeroAttack) {
        _actionDuration = _heroActionDuration;
        action = [self getMagicFinghtAction:_heroAtlas
                          textureNormalName:textureHeroNormalName
                                 finghtInfo:[MSFinghtActionInfo infoWithHeroType:type]
                                      fromx:CGRectGetWidth(self.frame)/4
                                        tox:-CGRectGetWidth(self.frame)/4
                                     weapon:weapon];
    } else {
        _actionDuration = _enemyActionDuration;
        action = [self getMagicFinghtAction:_enemyAtlas
                          textureNormalName:textureEnemyNormalName
                                 finghtInfo:[MSFinghtActionInfo infoWithEnemyType:type]
                                      fromx:-CGRectGetWidth(self.frame)/4
                                        tox:CGRectGetWidth(self.frame)/4
                                     weapon:weapon];
    }
    
    [self showFightEffect];
    return action;
}

- (SKAction *)getBowActionWithIsHeroAttack:(BOOL)isHeroAttack type:(NSInteger)type {
    SKAction *action;
    if (isHeroAttack) {
        _actionDuration = _heroActionDuration;
        action = [self getBowFinghtAction:_heroAtlas
                          textureNormalName:textureHeroNormalName
                                 finghtInfo:[MSFinghtActionInfo infoWithHeroType:type]
                                      fromx:CGRectGetWidth(self.frame)/4
                                        tox:-CGRectGetWidth(self.frame)/4];
    } else {
        _actionDuration = _enemyActionDuration;
        action = [self getBowFinghtAction:_enemyAtlas
                          textureNormalName:textureEnemyNormalName
                                 finghtInfo:[MSFinghtActionInfo infoWithEnemyType:type]
                                      fromx:-CGRectGetWidth(self.frame)/4
                                        tox:CGRectGetWidth(self.frame)/4];
    }
    
    [self showFightEffect];
    return action;
}

- (SKAction *)getActionWithIsHeroAttack:(BOOL)isHeroAttack type:(NSInteger)type {
    SKAction *action;
    if (isHeroAttack) {
        _actionDuration = _heroActionDuration;
        action = [self getFinghtAction:_heroAtlas
                     textureNormalName:textureHeroNormalName
                            finghtInfo:[MSFinghtActionInfo infoWithHeroType:type]
                                 fromx:CGRectGetWidth(self.frame)/4
                                   tox:-CGRectGetWidth(self.frame)/8];
    } else {
        _actionDuration = _enemyActionDuration;
        action = [self getFinghtAction:_enemyAtlas
                     textureNormalName:textureEnemyNormalName
                            finghtInfo:[MSFinghtActionInfo infoWithEnemyType:type]
                                 fromx:-CGRectGetWidth(self.frame)/4
                                   tox:CGRectGetWidth(self.frame)/8];
    }
    
    [self showFightEffect];
    return action;
}

- (void)showFightEffect {
    float y = ScreenHeight/2;
    SKAction *selfAction = [SKAction sequence:@[[SKAction waitForDuration:_delayAttakTime],
                                                [SKAction moveToY:y-7 duration:0.1],
                                                [SKAction moveToY:y+5 duration:0.1],
                                                [SKAction moveToY:y-2 duration:0.1],
                                                [SKAction moveToY:y duration:0.1]]];
    [self runAction:selfAction];
    
    SKAction *redAction = [SKAction sequence:@[[SKAction waitForDuration:_delayAttakTime],
                                               [SKAction fadeInWithDuration:0.1],
                                               [SKAction fadeOutWithDuration:0.1],
                                               [SKAction fadeInWithDuration:0.1],
                                               [SKAction fadeOutWithDuration:0.1]]];
    [_redNode runAction:redAction];
                        
}


- (NSArray *)getTextureArray:(NSInteger)from end:(NSInteger)end atlas:(SKTextureAtlas *)atlas {
    NSMutableArray *textureArray = [NSMutableArray array];
    for (NSInteger i=from; i<=end; i++) {
        SKTexture *texture = [atlas textureNamed:[NSString stringWithFormat:@"TextureImage_%d", i]];
        [textureArray addObject:texture];
    }
    
    return textureArray;
}

- (SKAction *)getBowFinghtAction:(SKTextureAtlas *)atlas
                 textureNormalName:(NSString *)textureNormalName
                        finghtInfo:(MSFinghtActionInfo *)info
                             fromx:(float)fromx
                               tox:(float)tox;
{
    // 动画分8个阶段
    // 1等待
    SKAction *action1 = [SKAction animateWithTextures:@[[atlas textureNamed:textureNormalName]] timePerFrame:0.5];
    
    // 2准备
    NSArray *textureArray2 = [self getTextureArray:0 end:info.maxPage atlas:atlas];
    SKAction *action2 = [SKAction animateWithTextures:textureArray2 timePerFrame:_actionDuration];
    
    // 3结束
    SKAction *action3 = [SKAction animateWithTextures:@[[atlas textureNamed:textureNormalName]] timePerFrame:0.2];
    SKAction *action = [SKAction sequence:@[action1,action2,action3]];
    
    _delayAttakTime = action1.duration + action2.duration;
    
    // 弓箭动画
    SKTexture *texture = [atlas textureNamed:@"TextureImage_Bow"];
    SKSpriteNode *sprite = [SKSpriteNode spriteNodeWithTexture:texture];
    sprite.size = CGSizeMake(200, 200);
    sprite.zPosition = 6;
    sprite.position = CGPointMake(fromx, 20);
    sprite.alpha = 0;
    [sprite runAction:[SKAction sequence:@[[SKAction fadeInWithDuration:_delayAttakTime],
                                           [SKAction moveToX:tox duration:0.2]]] completion:^{
        [sprite removeFromParent];
    }];
    [self addChild:sprite];
    
    return action;
}

- (SKAction *)getMagicFinghtAction:(SKTextureAtlas *)atlas
                 textureNormalName:(NSString *)textureNormalName
                        finghtInfo:(MSFinghtActionInfo *)info
                             fromx:(float)fromx
                               tox:(float)tox
                            weapon:(MSWeaponInfo *)weaponInfo;
{
    // 动画分8个阶段
    // 1等待
    SKAction *action1 = [SKAction animateWithTextures:@[[atlas textureNamed:textureNormalName]] timePerFrame:0.5];
    
    // 2准备
    NSArray *textureArray2 = [self getTextureArray:0 end:info.goBeigin-1 atlas:atlas];
    SKAction *action2 = [SKAction animateWithTextures:textureArray2 timePerFrame:_actionDuration];
    
    // 3前进
    NSArray *textureArray3 = [self getTextureArray:info.goBeigin end:info.goEnd atlas:atlas];
    SKAction *action3 = [SKAction animateWithTextures:textureArray3 timePerFrame:_actionDuration];
    
    // 4攻击
    NSArray *textureArray4 = [self getTextureArray:info.goEnd end:info.backEnd atlas:atlas];
    SKAction *action4 = [SKAction animateWithTextures:textureArray4 timePerFrame:_actionDuration];
    
    // 5
    NSArray *textureArray5 = [self getTextureArray:info.backEnd+1 end:info.maxPage atlas:atlas];
    SKAction *action5 = [SKAction animateWithTextures:textureArray5 timePerFrame:_actionDuration+0.1];
    
    // 6结束
    SKAction *action6 = [SKAction animateWithTextures:@[[atlas textureNamed:textureNormalName]] timePerFrame:0.2];
    SKAction *action = [SKAction sequence:@[action1,action2,action3,action4,action5, action6]];
    
    _delayAttakTime = action1.duration + action2.duration + action3.duration + action4.duration;
    
    // 魔法动画
    MSMagicSprite *sprite = [[MSMagicSprite alloc] initWithType:weaponInfo.model];
    sprite.size = CGSizeMake(100, 100);
    sprite.zPosition = 6;
    sprite.position = CGPointMake(fromx, 0);
    [sprite runActionWithFristDuration:_delayAttakTime attackDuration:action5.duration to:CGPointMake(tox, 0) block:^{
        [sprite removeFromParent];
    }];
    [self addChild:sprite];
    
    return action;
}

- (SKAction *)getFinghtAction:(SKTextureAtlas *)atlas
            textureNormalName:(NSString *)textureNormalName
                   finghtInfo:(MSFinghtActionInfo *)info
                        fromx:(float)fromx
                          tox:(float)tox
{
    // 动画分8个阶段
    // 1等待
    SKAction *action1 = [SKAction animateWithTextures:@[[atlas textureNamed:textureNormalName]] timePerFrame:0.5];
    
    // 2准备
    NSArray *textureArray2 = [self getTextureArray:0 end:info.goBeigin-1 atlas:atlas];
    SKAction *action2 = [SKAction animateWithTextures:textureArray2 timePerFrame:_actionDuration];
    
    // 3前进
    NSArray *textureArray3 = [self getTextureArray:info.goBeigin end:info.goEnd atlas:atlas];
    SKAction *action3_1 = [SKAction animateWithTextures:textureArray3 timePerFrame:_actionDuration];
    SKAction *action3_2 = [SKAction moveToX:tox duration:action3_1.duration];
    SKAction *action3 = [SKAction group:@[action3_1, action3_2]];
    
    // 4攻击
    NSArray *textureArray4 = [self getTextureArray:info.goEnd+1 end:info.backBeigin-1 atlas:atlas];
    SKAction *action4 = [SKAction animateWithTextures:textureArray4 timePerFrame:_actionDuration];
    
    // 5后退、返回
    NSArray *textureArray5 = [self getTextureArray:info.backBeigin end:info.backEnd atlas:atlas];
    SKAction *action5_1 = [SKAction animateWithTextures:textureArray5 timePerFrame:_actionDuration];
    SKAction *action5_2 = [SKAction moveToX:fromx duration:action5_1.duration];
    SKAction *action5 = [SKAction group:@[action5_1, action5_2]];
    
    // 6休整
    NSArray *textureArray6 = [self getTextureArray:info.backEnd+1 end:info.maxPage atlas:atlas];
    SKAction *action6 = [SKAction animateWithTextures:textureArray6 timePerFrame:_actionDuration];
    
    // 7结束
    SKAction *action7 = [SKAction animateWithTextures:@[[atlas textureNamed:textureNormalName]] timePerFrame:0.2];
    SKAction *action = [SKAction sequence:@[action1,action2,action3,action4,action5,action6,action7]];
    
    _delayAttakTime = action1.duration + action2.duration + action3.duration;
    
    return action;
}

- (void)doubleHitShow:(BOOL)isHeroAttack
                 hero:(MSHeroInfo *)hero
                enemy:(MSEnemyInfo *)enemy
               sprite:(SKSpriteNode *)sprite
               action:(SKAction *)action
               isBack:(BOOL)isback {
    float missHp = 0;
    NSInteger HP;
    NSInteger MAXHP;
    MSProgressBar *progressBar;
    CGFloat  otherx;
    if (isback) {
        isHeroAttack = !isHeroAttack;
    }
    if (isHeroAttack) {
        missHp = [[MSGameManager shareManager] attackHPEnemy:hero enemy:enemy];
        HP = enemy.HP;
        MAXHP = enemy.MaxHP;
        progressBar = _enemyProgressBar;
        otherx = -CGRectGetWidth(self.frame)/4;
    } else {
        missHp = [[MSGameManager shareManager] attackHPHero:hero enemy:enemy];
        HP = hero.HP;
        MAXHP = hero.MaxHP;
        progressBar = _heroProgressBar;
        otherx = CGRectGetWidth(self.frame)/4;
    }
    
    BOOL isMiss = (missHp==0);
    [sprite runAction:action completion:^{
        if (isMiss) {
            [self finishAction];
        } else {
            [progressBar.titleLabelNode setText:[NSString stringWithFormat:@"%d/%d", HP, MAXHP]];
            [progressBar setProgressWithAniamtion:HP/(float)MAXHP completion:^{
                [self finishAction];
            }];
        }
    }];
    
    if (isMiss) {
        [self performSelector:@selector(missShowWithPosition:) withObject:[NSValue valueWithCGPoint:CGPointMake(otherx, -20)] afterDelay:_delayAttakTime];
    }
};

- (void)missShowWithPosition:(NSValue *)point {
    SKLabelNode *label = [[SKLabelNode alloc] initWithFontNamed:@"Chalkduster"];
    [label setText:@"Miss"];
    [label setZPosition:4];
    [label setPosition:[point CGPointValue]];
    [label setFontSize:17];
    [label setFontColor:[UIColor redColor]];
    [self addChild:label];
    
    [label runAction:[SKAction moveToY:20 duration:0.7] completion:^{
        [label removeFromParent];
    }];
}

- (void)finishAction {
    [self removeFromParent];
    
    if (_block) {
        _block();
        _block = nil;
    }
};

@end
